package com.heima.article.listner;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.common.constant.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 消费任务
 */
@Component
@Slf4j
public class ArtilceIsDownListener {

    @Autowired
    private ApArticleConfigService apArticleConfigService;
    @Autowired
    private ApArticleService apArticleService;

    /**
     * 修改文章配置
     * @param message
     */
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(String message){
        log.info("【article模块监听到任务信息】:{}",message);
        if (message!=null){
            Map map = JSON.parseObject(message, Map.class);
            apArticleConfigService.updateByMap(map);
            log.info("article端文章配置修改，articleId={}",map.get("articleId"));
        }
    }

    /**
     * 修改文章频道名称
     */
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UPDATE_CHANNEL_TOPIC)
    public void updateChannelName(String message){
        log.info("【监听到消息】:修改文章频道名称:{}",message);
        WmChannel wmChannel = JSON.parseObject(message, WmChannel.class);
        apArticleService.update(Wrappers.<ApArticle>lambdaUpdate()
                .eq(ApArticle::getChannelId, wmChannel.getId())
                .set(ApArticle::getChannelName, wmChannel.getName()));
        log.info("【修改成功】");
    }
}
